package com.auxgroup.smarthome.entity;

import com.auxgroup.smarthome.session.SessionMenu;
import com.auxgroup.smarthome.session.SessionRole;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by kevinchen on 2016/12/24.
 */
@Entity
@Table(name = "sys_role")
public class RoleEntity extends AbstractGenericEntity {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(length = 36)
    private String id;

    @Column(length = 100, unique = true)
    private String roleName;

    /**这是一个特殊角色标识，在后台运营系统初始化的过程中自动生成与该特殊标识相关的角色*/
    @Column(length = 50,unique = true)
    private String specialMark;

    @Column
    private String roleDescription;

    @Enumerated(EnumType.STRING)
    @Column(length = 20)
    private RoleTag roleTag = RoleTag.COMMONROLE;

    private Boolean disable = false;

    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
    @JoinTable(name = "sys_role_permission", joinColumns = {
            @JoinColumn(name = "role_id")}, inverseJoinColumns = {
            @JoinColumn(name = "permission_id")})
    private Set<MenuEntity> menus;

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRoleDescription() {
        return roleDescription;
    }

    public void setRoleDescription(String roleDescription) {
        this.roleDescription = roleDescription;
    }

    public Set<MenuEntity> getMenus() {
        return menus;
    }

    public void setMenus(Set<MenuEntity> menus) {
        this.menus = menus;
    }

    public String getSpecialMark() {
        return specialMark;
    }

    public void setSpecialMark(String specialMark) {
        this.specialMark = specialMark;
    }

    public RoleTag getRoleTag() {
        return roleTag;
    }

    public void setRoleTag(RoleTag roleTag) {
        this.roleTag = roleTag;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Boolean getDisable() {
        return disable;
    }

    public void setDisable(Boolean disable) {
        this.disable = disable;
    }

    public RoleEntity() {
    }

    public RoleEntity(String roleName, String roleDescription, Set<MenuEntity> menus) {
        this.roleName = roleName;
        this.roleDescription = roleDescription;
        this.menus = menus;
    }

    public RoleEntity(String roleName,String roleDescription, RoleTag roleTag){
        this.roleName = roleName;
        this.roleDescription = roleDescription;
        this.roleTag = roleTag;
    }

    public SessionRole createSessionRole(){
        List<MenuEntity> menuEntities = new ArrayList<>(menus);
        List<SessionMenu> sessionMenus = new ArrayList<>();
        for (Iterator<MenuEntity> iterator = menuEntities.iterator();iterator.hasNext();){
            MenuEntity next = iterator.next();
            SessionMenu sessionMenu = next.createSessionMenu();
            sessionMenus.add(sessionMenu);
        }
        return new SessionRole(id, roleName, roleDescription,roleTag,sessionMenus, createdAt);
    }
}
